
//Marks the right margin of code *******************************************************************
package com.rmc.utils
{
	
	//--------------------------------------
	//  Imports
	//--------------------------------------
	import com.inruntime.utils.Global;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	
	
	//--------------------------------------
	//  Metadata
	//--------------------------------------
	
	//--------------------------------------
	//  Class
	//--------------------------------------
	/**
	 * <p>The <code>RMCGlobals</code> class is designed to hold sparse (and sparsely used) 
	 * global variables for INTERNAL use in the API.</p>
	 * 
	 * <p>AUTHOR 		: Samuel Asher Rivello</p>
	 * <p>COMPANY 		: Rivello Multimedia Consulting</p>
	 * <p>CREATION DATE 	: Nov 18, 2010</p>
	 * 
	 * @example Here is a code example. 
	 *  
	 * <listing version="3.0">
	 * <p>Describe this code example.</p>
	 *
	 * </listing>
	 *
	 * CLASS VISIBILITY :  INTERNAL : YES, USERS : NO
	 * @private
	 * 
	 */
	public class RMCGlobals
	{		
		
		//--------------------------------------
		//  Properties
		//--------------------------------------
		//PUBLIC GETTER/SETTERS
		
		//PUBLIC CONST
		/**
		 * Global Reference: The application's main <code>Stage</code>.
		 * 
		 */
		public static function get STAGE() 								                : Stage 	{ return Global.getInstance().stage;		}
		
		/**
		 * Global Reference: The application's outermost 'application' (the optional SystemManager, if in use, will be the parent of the application). Typically extends Sprite.
		 * 
		 */
		public static function get TOP_LEVEL_APPLICATION() 				              : DisplayObject 	{	RMCGlobals._DO_INITIALIZE(); 		  return Global.getInstance().topLevelApplication;		}
		public static function set TOP_LEVEL_APPLICATION(value : DisplayObject) 	: void 	        	{	RMCGlobals._DO_INITIALIZE(value);	}
		
		
		//PRIVATE
		
		//PRIVATE STATIC
		/**
		 * Ensures core functionality only runs one time.
		 * 
		 */
		private static var _isInitialized_boolean : Boolean = false;
		
		
		//--------------------------------------
		//  Constructor
		//--------------------------------------
		
		
		//--------------------------------------
		//  Methods
		//--------------------------------------		
		//PUBLIC	
		
		//PRIVATE	
		
		/**
		 * Called internally when the values of the properties are first set.
		 * 
		 * @param topLevelApplication_sprite Sprite of the main application
		 * 
		 * @return void
		 *  
		 */
		private static function _DO_INITIALIZE(topLevelApplication_displayobject : DisplayObject = null) : void 
		{
			
			//NOT YET INITIALIZED, THEN WE REQUIRE THAT topLevelApplication_displayobject WAS JUST SENT IN
			if (!_isInitialized_boolean) {
				//ONLY RUN ONE TIME
				_isInitialized_boolean = true;
				//
				if (topLevelApplication_displayobject) {
					
					//1. SET THE TOP LEVEL APP
					Global.getInstance().topLevelApplication 	= topLevelApplication_displayobject;
					
					//2. SET THE STAGE
					if (Global.getInstance().topLevelApplication.stage != null) {
						
						//If the SystemManager is in use (dynamically creates a preloader, its optional)
						//Then SystemManager is the master parent of the displaylist
						//if not, then the main application is the master parent of the display list
						//
						//IN BOTH CASES: set stage to a value that works and topLevelApplication to the Main Application (not SystemManager, ever)
						Global.getInstance().stage 					      = Global.getInstance().topLevelApplication.stage;
					} else {
						throw new Error ("Global.getInstance().topLevelApplication.stage is required, but is : " + Global.getInstance().topLevelApplication.stage);
					}
					
				} else {
					
					//WE ARE ALREADY INITIALIZED, SO JUST MAKE SURE WE ALREADY HAVE THE VARS WE NEED. SHOULD BE FINE 100%
					if (!Global.getInstance().topLevelApplication) {
						throw new Error("RMCGlobals.TOP_LEVEL_APPLICATION must be set before RMCGlobals can be used. Simply set it (RMCGlobals.TOP_LEVEL_APPLICATION=this) in the main application's constructor.");
					}
				}
				
				//ALREADY INITIALIZED WELL THEN WE BETTER HAVE NOTHING PASSED IN FOR 
			} else {
				if (topLevelApplication_displayobject) {
					throw new Error("RMCGlobals.TOP_LEVEL_APPLICATION is already set. Do not pass in an argument after initialization.");
				}
			}
		}
		
		
		//--------------------------------------
		//  Event Handlers
		//--------------------------------------		
	}
}